﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace TestEngine
{
    [Serializable]
    public class TestScript : IEnumerable<ScriptTask>
    {

        #region Private Members
        List<ScriptTask> _script = new List<ScriptTask>();
        #endregion Private Members

        #region Public Properties

        public int Count
        {
            get
            {
               return _script.Count;
            }
        }

        #endregion Public Properties

        #region Public Methods

        public void Add(ScriptTask task)
        {
            _script.Add(task);
        }

        public static TestScript Load(string scriptFile)
        {
            using (XmlReader textReader = XmlReader.Create(scriptFile))
            {
                return new XmlSerializer(typeof (TestScript)).Deserialize(textReader) as TestScript;
            }
        }

        public void Save(string scriptFile)
        {
            using (XmlWriter textWriter = XmlWriter.Create(scriptFile))
            {
                new XmlSerializer(typeof (TestScript)).Serialize(textWriter, this);
            }
        }

        #region IEnumerable Methods
        
        /// <summary>
        /// Returns each task in the script in order they should be executed.
        /// </summary>
        /// <returns></returns>
        public IEnumerator<ScriptTask> GetEnumerator()
        {
            foreach(ScriptTask task in _script)
            {
                yield return task;
            }
        }

        /// <summary>
        /// Returns each task in the script in order they should be executed.
        /// </summary>
        /// <returns></returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion IEnumerable Methods
        
        #endregion Public Methods
    }
}
